﻿using System.Drawing;
using System.Windows.Forms;

namespace demo07_Font与Text
{
    public partial class Form2 : Form
    {
        //在构造函数中添加代码
        public Form2()
        {
            InitializeComponent();
            this.BackColor = Color.White;
            this.AutoScroll = true;
        }

        //重写窗体 Form1 的 OnPaint()方法
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            //拖动滚动条时，变换坐标原点
            g.TranslateTransform(AutoScrollPosition.X, AutoScrollPosition.Y);
            Point position = new Point(5, 0); //用来控制文本的输出位置
            foreach (FontFamily family in FontFamily.Families)
            {
                if (family.IsStyleAvailable(FontStyle.Regular))
                {
                    Font font = new Font(family.Name, 10);
                    //输出字体名称
                    g.DrawString(family.Name, font, Brushes.Black, position);
                    position.Y += font.Height + 4;
                    font.Dispose(); //释放 font 对象
                }
            }
            //校正滚动条范围
            this.AutoScrollMinSize = new Size(300, position.Y);
        }
    }
}
